package logic

import (
	"beike/models"
	"context"

	"beike/houserpc/houserpc"
	"beike/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ResListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewResListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResListLogic {
	return &ResListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ResListLogic) ResList(in *houserpc.ResListRequest) (*houserpc.ResListResponse, error) {
	// todo: add your logic here and delete this line

	var ress []models.UserHouseUserHouse
	tx := models.DB.Debug().Model(&models.Reservation{}).
		Select("reservations.*,users.nick_name as nick_name,users.mobile as mobile,houses.address as address,houses.area as area,houses.types as types").
		Joins("left join users on users.id = reservations.user_id").
		Joins("left join houses on houses.id = reservations.house_id")

	if in.CreatedAtOrder == 1 {
		tx = tx.Order("created_at desc")
	}

	if in.CreatedAtOrder == 2 {
		tx = tx.Order("created_at asc")
	}

	tx.Find(&ress)

	var resList []*houserpc.ResList

	for _, res := range ress {
		list := houserpc.ResList{
			NickName: res.NickName,
			Mobile:   res.Mobile,
			Address:  res.Address,
			Types:    res.Types,
			Area:     float32(res.Area),
			ResTime:  res.ResTime,
			UserId:   res.UserId,
			HouseId:  res.HouseId,
		}
		resList = append(resList, &list)
	}

	return &houserpc.ResListResponse{
		List: resList,
	}, nil
}
